package com.hxzy.projectssmplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.projectssmplus.dto.SysRoleDTO;
import com.hxzy.projectssmplus.entity.SysRole;
import com.hxzy.projectssmplus.mapper.SysRoleMapper;
import com.hxzy.projectssmplus.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.List;
import java.util.Set;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {



    @Override
    public IPage<SysRole> search(IPage ipage, SysRoleDTO sysRoleDTO) {
        QueryWrapper<SysRole> queryWrapper=new QueryWrapper();
        if(StringUtils.isNotBlank( sysRoleDTO.getName())){
            queryWrapper.like("name",  sysRoleDTO.getName());
        }

        if(StringUtils.isNotBlank( sysRoleDTO.getPerms())){
            queryWrapper.like("perms",  sysRoleDTO.getPerms());
        }

        if( sysRoleDTO.getStatus()!=null){
            queryWrapper.eq("status", sysRoleDTO.getStatus());
        }
         //日期范围
        if(sysRoleDTO.getMap().size()==2){
            //DATE_FORMAT(create_time,'%Y%m%d')
            String beginStr="DATE_FORMAT('"+sysRoleDTO.getMap().get("begin")+"','%Y%m%d')";
            String endStr="DATE_FORMAT('"+sysRoleDTO.getMap().get("end")+"','%Y%m%d')";
            //queryWrapper.between("DATE_FORMAT(create_time,'%Y%m%d')",  beginStr, endStr);
            //有sql注入风险
            queryWrapper.apply("date_format(create_time,'%Y%m%d') between   "+beginStr+"    and "+endStr);

        }

        IPage<SysRole> page =  super.page(ipage, queryWrapper);

        return page;
    }

    @Autowired
    private SysRoleMapper  sysRoleMapper;

    @Override
    public IPage<SysRole> searchByXML(IPage ipage, SysRoleDTO sysRoleDTO) {
        List<SysRole> sysRoleList=sysRoleMapper.searchByXML(ipage,sysRoleDTO);
        //满足条件集合，自已手动加入到ipage类中去
        ipage.setRecords(sysRoleList);

        return ipage;
    }

    /**
     * 根据用户id查询它拥有的角色 perms这一列(多表联接，还是使用最原始的mybatis)
     * @param adminUserId
     * @return
     */
    @Override
    public Set<String> ownerRoleByUserId(Integer adminUserId) {
        return this.sysRoleMapper.ownerRoleByUserId(adminUserId);

    }
}
